package com.lagou.server;

import java.net.Socket;
import java.util.Map;

@SuppressWarnings("all")
public class RequestProccessor extends Thread {
    /**
     * socket（每个请求一个socket）
     */
    private Socket socket;
    /**
     * servlet容器
     */
    private Map<String, HttpServlet> servletMap;

    @Override
    public void run() {
        try {
            //封装request对象
            Request request = new Request(socket.getInputStream());
            //封装response对象
            Response response = new Response(socket.getOutputStream());
            if (servletMap.containsKey(request.getUrl())) {//动态资源
                HttpServlet httpServlet = servletMap.get(request.getUrl());
                httpServlet.service(request, response);
            } else {//静态资源
                response.outputHtml(request.getUrl());
            }
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public RequestProccessor(Socket socket, Map<String, HttpServlet> servletMap) {
        this.socket = socket;
        this.servletMap = servletMap;
    }
}
